/*
 * multi ir daemon for android -
 * Copyright (C) 2015-2018 AllwinnerTech
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <sys/ioctl.h>
#include <linux/uinput.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
  
#include <errno.h>
#include <stdio.h>
#include <string.h>

#define UINPUT_NAME		"/dev/uinput"
#define INPUTDEV_NAME	"sunxi-ir"

static int uinputfd = -1;
static struct uinput_user_dev virtual_inputdev;

int setup_virtual_input_dev(void)
{
	int i;

	uinputfd = open(UINPUT_NAME, O_WRONLY | O_NDELAY);
	if (uinputfd <= 0) {
		fprintf(stderr, "could not open '%s', %s\n", UINPUT_NAME, strerror(errno));
		return uinputfd;
	}

	memset(&virtual_inputdev, 0, sizeof(virtual_inputdev));
	strncpy(virtual_inputdev.name, INPUTDEV_NAME, UINPUT_MAX_NAME_SIZE);
	virtual_inputdev.id.version = 4;
	virtual_inputdev.id.bustype = BUS_USB;

    ioctl(uinputfd, UI_SET_EVBIT, EV_KEY);
    for (i = 0; i < 256; i++)
		ioctl(uinputfd, UI_SET_KEYBIT, i);

	/* setup mouse coordinate event */
    ioctl(uinputfd, UI_SET_EVBIT,  EV_REL);
    ioctl(uinputfd, UI_SET_RELBIT, REL_X);
    ioctl(uinputfd, UI_SET_RELBIT, REL_Y);

    /* setup mouse button event */
    ioctl(uinputfd, UI_SET_KEYBIT, BTN_MOUSE);
    ioctl(uinputfd, UI_SET_KEYBIT, BTN_TOUCH);
    ioctl(uinputfd, UI_SET_KEYBIT, BTN_LEFT);
    ioctl(uinputfd, UI_SET_KEYBIT, BTN_RIGHT);
    ioctl(uinputfd, UI_SET_KEYBIT, BTN_MIDDLE);

    /* create input device into input subsystem */
    write(uinputfd, &virtual_inputdev, sizeof(virtual_inputdev));
    if (ioctl(uinputfd, UI_DEV_CREATE)) {
		fprintf(stderr, "could not create uinput device, %s\n", strerror(errno));
        close(uinputfd);
        uinputfd = -1;
		return uinputfd;
    }

	return 0;
}


void report_key_event(void)   
{
	struct input_event event;

	memset(&event, 0, sizeof(event));   
	gettimeofday(&event.time, NULL);   
	event.type = EV_KEY;   
	event.code = KEY_A;   
	event.value = 1;   
	write(uinputfd, &event, sizeof(event));   
	  
	event.type = EV_SYN;   
	event.code = SYN_REPORT;   
	event.value = 0;   
	write(uinputfd, &event, sizeof(event));   
	  
	// Report BUTTON CLICK - RELEASE event   
	memset(&event, 0, sizeof(event));   
	gettimeofday(&event.time, NULL);   
	event.type = EV_KEY;   
	event.code = KEY_A;   
	event.value = 0;
	write(uinputfd, &event, sizeof(event));

	event.type = EV_SYN;   
	event.code = SYN_REPORT;   
	event.value = 0;   
	write(uinputfd, &event, sizeof(event));   
}
